﻿using System;
using System.Collections.Generic;
using System.Web;
using UniversityManagementSystem.DAL;
using UniversityManagementSystem.Gateway;

namespace UniversityManagementSystem.Manager
{
    public class ClassScheduleManager
    {
        public string SaveSchedule(ClassSchedule aClassSchedule)
        {
            string saveClassSchedule = "";
            try
            {
                ClassScheduleGateway gateway = new ClassScheduleGateway();
                DepartmentGateway departmentGateway = new DepartmentGateway();
                
                bool checkRoomAvailable = false;
                bool timeOverlapCheck = false;
                bool timeOverlapTeacher = false;

                List<ClassSchedule> getClassSchedule = new List<ClassSchedule>();
                List<ClassSchedule> getClassScheduleTeacher = new List<ClassSchedule>();
                getClassSchedule = gateway.GetClassSchedule(aClassSchedule);
                //timeOverlapCheck = TimeOverlapCheck(getClassSchedule,aClassSchedule);
                if (getClassSchedule.Count != 0)
                {
                    saveClassSchedule = "The selected time is overlapping an existing course schedule";
                }
                else
                {
                    getClassScheduleTeacher = gateway.GetClassScheduleTeacher(aClassSchedule);
                    if (getClassScheduleTeacher.Count == 0)
                    {
                        checkRoomAvailable = gateway.SaveSchedule(aClassSchedule);
                    }
                    else
                    {
                        saveClassSchedule = "Teacher's Time Overlaps.";
                    }
                    if (checkRoomAvailable)
                    {
                        saveClassSchedule = "Class Schedule has been added successfully";
                    }  
                }
                              
            }
            catch (Exception exception)
            {
                throw new Exception("Schedule cannot be added",exception);
            }
            return saveClassSchedule;
            
        }

        //private bool TimeOverlapCheck(List<ClassSchedule> getClassSchedule,ClassSchedule aClassSchedule)
        //{
        //    bool timeOverlapCheck = false;
        //    try
        //    {
        //        foreach (ClassSchedule classSchedule in getClassSchedule)
        //        {
        //            if ((classSchedule.ClassStarTime.Hours == aClassSchedule.ClassStarTime.Hours))
        //            {
        //                if ((aClassSchedule.ClassStarTime.Minutes <= classSchedule.ClassStarTime.Minutes) &&
        //                             (aClassSchedule.ClassEndTime.Minutes >= classSchedule.ClassStarTime.Minutes))
        //                {
        //                    timeOverlapCheck = true;
        //                }
        //                if (classSchedule.ClassEndTime.Hours == aClassSchedule.ClassStarTime.Hours)
        //                {
        //                    if ((classSchedule.ClassStarTime.Minutes <= aClassSchedule.ClassStarTime.Minutes) &&
        //                        (classSchedule.ClassEndTime.Minutes >= aClassSchedule.ClassStarTime.Minutes))
        //                    {
        //                        timeOverlapCheck = true;
        //                    }
        //                    else if ((aClassSchedule.ClassStarTime.Minutes <= classSchedule.ClassStarTime.Minutes) &&
        //                             (aClassSchedule.ClassEndTime.Minutes >= classSchedule.ClassStarTime.Minutes))
        //                    {
        //                        timeOverlapCheck = true;
        //                    }
        //                }
        //            }
        //            else if (aClassSchedule.ClassStarTime.Hours > classSchedule.ClassStarTime.Hours)
        //            {
        //                if (aClassSchedule.ClassStarTime.Hours < classSchedule.ClassEndTime.Hours)
        //                {
        //                    timeOverlapCheck = true;
        //                }
        //                else if (aClassSchedule.ClassStarTime.Hours == classSchedule.ClassEndTime.Hours)
        //                {
        //                    if (aClassSchedule.ClassStarTime.Minutes <= classSchedule.ClassEndTime.Minutes)
        //                    {
        //                        timeOverlapCheck = true;
        //                    }
        //                }
        //            }
        //            else if (aClassSchedule.ClassStarTime.Hours < classSchedule.ClassStarTime.Hours)
        //            {
        //                if (aClassSchedule.ClassEndTime.Hours >= classSchedule.ClassStarTime.Hours)
        //                {
        //                    timeOverlapCheck = true;
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception exception)
        //    {
                
        //        throw new Exception("Error Occured",exception);
        //    }

        //    return timeOverlapCheck;
        //}
    }
}